package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author kcs
 * @date 2021/11/27
 * @TIME:10:46
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */

    @CachePut(value = "SETMEAL_CACH",key = "'SETMEAL'+'_'+#setmealDto.categoryId+'_'+#setmealDto.status")
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto){
        setmealService.add(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 套餐信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findByPage(Integer page,Integer pageSize,String name){
        IPage<SetmealDto> iPage = setmealService.findByPage(page,pageSize,name);
        return R.success(iPage);
    }

    /**
     * 套餐管理 -- 批量起售/批量停售
     * @param status
     * @param ids
     * @return
     */
    @CacheEvict(value = "SETMEAL_CACH",allEntries = true) //清除SETMEAL_CACH名称下,所有的缓存数据
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status,Long[] ids){
        setmealService.updateStatus(status,ids);
        return R.success("操作成功");
    }

    /**
     * 套餐管理 -- 批量删除
     * @param ids
     * @return
     */
    @CacheEvict(value = "SETMEAL_CACH",allEntries = true) //清除SETMEAL_CACH名称下,所有的缓存数据
    @DeleteMapping
    public R delete(Long[] ids){
        setmealService.delete(ids);
        return R.success("删除成功");
    }

    /**
     * 套餐管理 -- 修改套餐(回显)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        return setmealService.findById(id);
    }

    /**
     * 套餐管理 -- 修改套餐(修改)
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "SETMEAL_CACH",allEntries = true) //清除SETMEAL_CACH名称下,所有的缓存数据
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 根据分类ID查询套餐(前台菜品展示)
     * @param setmeal
     * @return
     */
    @Cacheable(value = "SETMEAL_CACH",key = "'SETMEAL'+'_'+#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R list(Setmeal setmeal){
        List<Setmeal> setmeals = setmealService.listSetmeal(setmeal);
        return R.success(setmeals);
    }

    @GetMapping("/dish/{setmealId}")
    public R findDish(@PathVariable Long setmealId) {

        List<SetmealDish> setmealDishes = setmealDishService.list(
                new LambdaQueryWrapper<SetmealDish>()
                        .eq(SetmealDish::getSetmealId, setmealId));

        List<Long> dishIds = new ArrayList<>();

        setmealDishes = setmealDishes.stream().map(c -> {
            Long dishId = c.getDishId();
            dishIds.add(dishId);
            return c;
        }).collect(Collectors.toList());

        List<Dish> dishes = dishService.listByIds(dishIds);

        return R.success(dishes);
    }


}
